import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CustomImage extends ExtendedImage {
  CustomImage.network(
    url, {
    Key key,
    double width,
    double height,
    Color color,
    BlendMode colorBlendMode,
    BoxFit fit = BoxFit.cover,
    BoxShape shape = BoxShape.rectangle,
    BoxBorder border,
    BorderRadiusGeometry borderRadius,
    LoadStateChanged loadStateChanged,
    Color loadingViewBackgroundColor = const Color(0xffefefef),
    bool isResizeImage = false,
    double scale = 1.5,
    bool enableMemoryCache = true,
    bool clearMemoryCacheIfFailed = true,
    bool clearMemoryCacheWhenDispose = false,
    int retries = 3,
  }) : super(
          key: key,
          image: isResizeImage
              ? ResizeImage.resizeIfNeeded(
                  width == null ? null : (width * scale).ceil(),
                  height == null ? null : (height * scale).ceil(),
                  ExtendedNetworkImageProvider(
                    url ?? '',
                    cache: true,
                    retries: retries,
                  ),
                )
              : ExtendedNetworkImageProvider(
                  url ?? '',
                  cache: true,
                  retries: retries,
                ),
          width: width,
          height: height,
          color: color,
          colorBlendMode: colorBlendMode,
          fit: fit,
          enableMemoryCache: enableMemoryCache,
          clearMemoryCacheIfFailed: clearMemoryCacheIfFailed,
          clearMemoryCacheWhenDispose: clearMemoryCacheIfFailed,
          shape: shape,
          border: border,
          borderRadius: borderRadius,
          loadStateChanged: loadStateChanged ??
              (ExtendedImageState state) {
                switch (state.extendedImageLoadState) {
                  case LoadState.loading:
                    return Container(
                      width: width,
                      height: height,
                      decoration: BoxDecoration(
                        borderRadius: borderRadius,
                        color: Color(0xf5f5f5f5),
                      ),
                    );
                    break;
                  case LoadState.failed:
                    return Container(
                      width: width,
                      height: height,
                      decoration: BoxDecoration(
                        borderRadius: borderRadius,
                        color: Color(0xf5f5f5f5),
                      ),
                      child: Center(
                        child: Icon(Icons.error,size: width/10,),
                      ),
                    );
                    break;
                  default:
                    return null;
                    break;
                }
              },
        );
}
